package com.gec.yhzh.controller;


import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gec.yhzh.Exception.UserAlreadyExistsException;
import com.gec.yhzh.entity.User;
import com.gec.yhzh.entity.UserDTO;
import com.gec.yhzh.service.IUserService;
import com.gec.yhzh.service.QRService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 患者用户 前端控制器
 * </p>
 *
 * @author jerry
 * @since 2025-03-10
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    QrConfig config;
    @Autowired
    private IUserService userService;
    @Autowired
    private QRService qrService;

    //JSON解析工具
    ObjectMapper objectMapper = new ObjectMapper();

    @GetMapping("/queryAll")
    public String queryAllUser() throws JsonProcessingException {
        Map<String, Object> result = new HashMap<>();
        final List<User> userList = userService.list();
        result.put("result", userList);
        return objectMapper.writeValueAsString(result);
    }
    @GetMapping("/selectById")
    public String selectById(
            @RequestParam Integer id
    ) throws JsonProcessingException {
        Map<String, Object> result = new HashMap<>();
        User user = userService.getById(id);
        result.put("result", user);
        return objectMapper.writeValueAsString(result);
    }
    @PostMapping("/insertUser")
    public String insertUser(@RequestBody User user) throws JsonProcessingException {
        Map<String, Object> result = new HashMap<>();
        boolean isSave = userService.save(user);
        result.put("result", isSave);
        return objectMapper.writeValueAsString(result);
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(Integer id) throws  JsonProcessingException{
        Map<String, Object> result = new HashMap<>();
        boolean isRemove = userService.removeById(id);
        result.put("result", isRemove);
        return objectMapper.writeValueAsString(result);
    }

    @PostMapping("/updateUser")
    public String updateUser(@RequestBody User user) throws  JsonProcessingException{
        Map<String, Object> result = new HashMap<>();
        boolean isSave = userService.updateById(user);
        result.put("result", isSave);
        return objectMapper.writeValueAsString(result);
    }

    @GetMapping("/getUserById")
    public String getUserById(Integer id) throws JsonProcessingException {
        Map<String, Object> result = new HashMap<>();
        User user = userService.getUserById(id);
        result.put("result", user);
        return objectMapper.writeValueAsString(result);
    }
    @PostMapping(value="/loginUser",consumes = "application/x-www-form-urlencoded")
    public String loginUser(@RequestParam  String phone,@RequestParam String password) throws JsonProcessingException {
        //JSON解析工具
        Map<String, Object> result = new HashMap<>();
        User user= userService.Login(phone,password);
        if(user!=null){
        result.put("flag",true);
        result.put("user",user);
        result.put("role","用户");}
        else{
            result.put("flag",false);
        }
        return objectMapper.writeValueAsString(result);
    }
    @PostMapping(value = "/register", consumes = "application/x-www-form-urlencoded")
    public String register(@ModelAttribute UserDTO userDTO) throws JsonProcessingException {

        Map<String, Object> result = new HashMap<>();

        try {
            boolean flag = userService.register(userDTO);
            result.put("flag", flag);
            result.put("message", "注册成功");

            if (flag) {
                return objectMapper.writeValueAsString(result);
            } else {
                result.put("message", "注册失败");
                return objectMapper.writeValueAsString(result);
            }
        } catch (UserAlreadyExistsException e) {
            result.put("flag", false);
            result.put("message", e.getMessage());
            return objectMapper.writeValueAsString(result);
        } catch (IllegalArgumentException e) {
            result.put("flag", false);
            result.put("message", e.getMessage());
            return objectMapper.writeValueAsString(result);
        } catch (Exception e) {
            result.put("flag", false);
            result.put("message", "系统错误，请联系管理员");
            return objectMapper.writeValueAsString(result);
        }
    }
//生成二维码
    @RequestMapping("/code")
    public void generateUserCode(@RequestParam Long userid, HttpServletResponse response) throws IOException {
        User user =this.userService.getById(userid);
        if(user.getQrcode()== 1)
    {
        config.setBackColor(Color.green.getRGB());
    }
        else if(user.getQrcode()== 2)
        {
        config.setBackColor(Color.yellow.getRGB());
        }else
        {
        config.setBackColor(Color.red.getRGB());

        }
        qrService.generatorQRCode(userid,response);
        QrCodeUtil.generate(objectMapper.writeValueAsString(user),config,"png",response.getOutputStream());
    }
}
